package org.ptkparty.client.view.games;

import java.util.List;

import org.ptkparty.client.activity.ResultsActivity;
import org.ptkparty.client.resources.CellTableResource;
import org.ptkparty.client.resources.Resources;
import org.ptkparty.client.view.players.PlayerCell;
import org.ptkparty.shared.dto.ResultDto;

import com.google.gwt.cell.client.ImageResourceCell;
import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.cell.client.SafeHtmlCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class ResultsView extends Composite {

	private static ResultsViewUiBinder uiBinder = GWT.create(ResultsViewUiBinder.class);

	interface ResultsViewUiBinder extends UiBinder<Widget, ResultsView> {
	}

	private ListDataProvider<ResultDto> provider;

	@UiField(provided = true)
	CellTable<ResultDto> resultsTable;

	private ResultsActivity activity;

	public ResultsView() {
		resultsTable = new CellTable<ResultDto>(Integer.MAX_VALUE, CellTableResource.INSTANCE);
		final SingleSelectionModel<ResultDto> sm = new SingleSelectionModel<ResultDto>();
		sm.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				activity.playerSelected(sm.getSelectedObject().getUserId());
			}
		});
		resultsTable.setSelectionModel(sm);

		provider = new ListDataProvider<ResultDto>();
		provider.addDataDisplay(resultsTable);
		resultsTable.setAutoHeaderRefreshDisabled(true);

		ListHandler<ResultDto> sorts = new ListHandler<ResultDto>(provider.getList());
		resultsTable.addColumnSortHandler(sorts);

		Column<ResultDto, ImageResource> colImg = new Column<ResultDto, ImageResource>(new ImageResourceCell()) {

			@Override
			public ImageResource getValue(ResultDto object) {
				if (object.getPosition() < object.getLastPosition()) {
					return Resources.resources.getResultsUp();
				} else if (object.getPosition() > object.getLastPosition()) {
					return Resources.resources.getResultsDown();
				} else {
					return Resources.resources.getResultsEqual();
				}
			}
		};
		resultsTable.addColumn(colImg, "");
		resultsTable.setColumnWidth(colImg, "50px");

		Column<ResultDto, Number> col = new Column<ResultDto, Number>(new NumberCell()) {

			@Override
			public Number getValue(ResultDto object) {
				return object.getPosition();
			}
		};
		resultsTable.addColumn(col, "Position");

		resultsTable.addColumn(new Column<ResultDto, SafeHtml>(new SafeHtmlCell()) {

			@Override
			public SafeHtml getValue(ResultDto object) {
				if (object.getUserImage() != null) {
					return PlayerCell.template.playerTemplate(object.getUserImage(), object.getUser());
				} else {
					return PlayerCell.template.playerTemplate(object.getUser());
				}
			}
		}, "Joueur");

		resultsTable.addColumn(new Column<ResultDto, Number>(new NumberCell()) {

			@Override
			public Number getValue(ResultDto object) {
				return object.getPoints();
			}
		}, "Points");

		resultsTable.addColumn(new Column<ResultDto, Number>(new NumberCell()) {

			@Override
			public Number getValue(ResultDto object) {
				return object.getWinGames();
			}
		}, "Victoires");

		resultsTable.addColumn(new Column<ResultDto, Number>(new NumberCell()) {

			@Override
			public Number getValue(ResultDto object) {
				return object.getLooseGames();
			}
		}, "Défaites");

		resultsTable.addColumn(new Column<ResultDto, Number>(new NumberCell()) {

			@Override
			public Number getValue(ResultDto object) {
				return object.getFannyWinGames();
			}
		}, "Fanny gagnés");

		resultsTable.addColumn(new Column<ResultDto, Number>(new NumberCell()) {

			@Override
			public Number getValue(ResultDto object) {
				return object.getFannyLooseGames();
			}
		}, "Fanny encaissés");

		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	protected void onLoad() {
		super.onLoad();
		DivElement fbCommentDiv = (DivElement) Document.get().getElementById("fbcomment");
		fbCommentDiv.setInnerHTML("<fb:comments href=\"" + Window.Location.getHref() + "\" num_posts=\"10\"></fb:comments>");
		DivElement gPlusCommentDiv = (DivElement) Document.get().getElementById("gpluscomment");
		gPlusCommentDiv.setInnerHTML("<g:comments href=\"" + Window.Location.getHref() + "\" first_party_property=\"BLOGGER\" view_type=\"FILTERED_POSTMOD\"></g:comments>");
	}

	public void loadData(List<ResultDto> result) {
		provider.setList(result);
	}

	public void setActivity(ResultsActivity resultsActivity) {
		this.activity = resultsActivity;
	}

}
